/**
  ******************************************************************************
  * File Name          : USART.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "usart.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;

uint8_t aRxBuffer1 = 0;
uint8_t Uart1_RxBuff[256] = {0};
uint16_t Uart1_Rx_Cnt = 0;
uint8_t bRx1_complete = 0;
uint8_t aRxBuffer2 = 0;
uint8_t Uart2_RxBuff[256] = {0};
uint16_t Uart2_Rx_Cnt = 0;
uint8_t bRx2_complete = 0;
/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }

}
/* USART2 init function */

void MX_USART2_UART_Init(void)
{

  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
  
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration    
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX 
    */
    GPIO_InitStruct.Pin = U1_TX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(U1_TX_GPIO_Port, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = U1_RX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(U1_RX_GPIO_Port, &GPIO_InitStruct);

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspInit 0 */

  /* USER CODE END USART2_MspInit 0 */
    /* USART2 clock enable */
    __HAL_RCC_USART2_CLK_ENABLE();
  
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART2 GPIO Configuration    
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX 
    */
    GPIO_InitStruct.Pin = U2_TX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(U2_TX_GPIO_Port, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = U2_RX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(U2_RX_GPIO_Port, &GPIO_InitStruct);

    /* USART2 interrupt Init */
    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspInit 1 */

  /* USER CODE END USART2_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();
  
    /**USART1 GPIO Configuration    
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX 
    */
    HAL_GPIO_DeInit(GPIOA, U1_TX_Pin|U1_RX_Pin);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
  else if(uartHandle->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspDeInit 0 */

  /* USER CODE END USART2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART2_CLK_DISABLE();
  
    /**USART2 GPIO Configuration    
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX 
    */
    HAL_GPIO_DeInit(GPIOA, U2_TX_Pin|U2_RX_Pin);

    /* USART2 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART2_IRQn);
  /* USER CODE BEGIN USART2_MspDeInit 1 */

  /* USER CODE END USART2_MspDeInit 1 */
  }
} 

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == USART1)	// 判断是由哪个串口触发的中断
	{
		// HAL_GPIO_TogglePin(Beep_GPIO_Port,Beep_Pin);
        if(0 == bRx1_complete)
        {
            if(Uart1_Rx_Cnt < sizeof(Uart1_RxBuff) - 1)
            {
				Uart1_RxBuff[Uart1_Rx_Cnt] = aRxBuffer1;
				Uart1_Rx_Cnt++;
//				Return  = CR = 13 = '\x0d' = '\r'
//				NewLine = LF = 10 = '\x0a' = '\n'
				if(('\n' == Uart1_RxBuff[Uart1_Rx_Cnt-1])&&('\r' == Uart1_RxBuff[Uart1_Rx_Cnt-2]))
				{
					Uart1_RxBuff[Uart1_Rx_Cnt] = 0;
					Uart1_Rx_Cnt= 0;
					bRx1_complete = 1;
				}
            }
            else
            {
                Uart1_Rx_Cnt = 0;
                memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff));
            }
        }
//		else
//		{
//			if (flag_download && (aRx1Buffer == 'S'))
//			{
//				flag_stop_download = 1;
//			}
//		}
		HAL_UART_Receive_IT(&huart1,(uint8_t *)&aRxBuffer1,1);		// 重新使能串口1接收中断
	}
	if(huart->Instance == USART2)
	{
//		HAL_GPIO_TogglePin(Beep_GPIO_Port,Beep_Pin);
        if(0 == bRx2_complete)
        {
            if(Uart2_Rx_Cnt < sizeof(Uart2_RxBuff) - 1)
            {
				Uart2_RxBuff[Uart2_Rx_Cnt] = aRxBuffer2;
				Uart2_Rx_Cnt++;
//				Return  = CR = 13 = '\x0d' = '\r'
//				NewLine = LF = 10 = '\x0a' = '\n'
				if(('\n' == Uart2_RxBuff[Uart2_Rx_Cnt-1])&&('\r' == Uart2_RxBuff[Uart2_Rx_Cnt-2]))
				{
					Uart2_RxBuff[Uart2_Rx_Cnt] = 0;
					Uart2_Rx_Cnt= 0;
					bRx2_complete = 1;
				}
            }
            else
            {
                Uart2_Rx_Cnt = 0;
                memset(Uart2_RxBuff,0x00,sizeof(Uart2_RxBuff));
            }
        }
//		else
//		{
//			if (flag_download && (aRx1Buffer == 'S'))
//			{
//				flag_stop_download = 1;
//			}
//		}
		HAL_UART_Receive_IT(&huart2,(uint8_t *)&aRxBuffer2,1);		// 重新使能串口1接收中断
	}
}
//void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
//{
//	;
//}
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
